package ltd.dujiabao.dex.jdk.exe;

import java.util.concurrent.ScheduledThreadPoolExecutor;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;

import lombok.extern.slf4j.Slf4j;
import ltd.dujiabao.dex.core.DelayJobEntity;
import ltd.dujiabao.dex.core.emitter.Emitter;
import ltd.dujiabao.dex.core.emitter.EmitterFactory;
import ltd.dujiabao.dex.core.ExecutorType;

/**
 * @author : DuJiabao
 * @Project : common-delay-executor
 * @Time : 2024/3/30 11:39
 * @Desc :
 */
@Slf4j
public class JdkEmitterFactory implements EmitterFactory, BeanFactoryAware {
    private final ScheduledThreadPoolExecutor executor;
    private BeanFactory beanFactory;

    public JdkEmitterFactory(ScheduledThreadPoolExecutor executor) {
        this.executor = executor;
    }

    @Override
    public Emitter getEmitter(DelayJobEntity delayJob) {
        return new JdkEmitter(delayJob, executor, beanFactory);
    }

    @Override
    public ExecutorType executorType() {
        return ExecutorType.JDK;
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = beanFactory;
    }
}
